package com.kingdee.eas.scm.sm.pur.client;

import java.awt.event.ActionEvent;
import java.util.Iterator;
import java.util.Set;
import com.kingdee.bos.BOSException;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.dao.query.IQueryExecutor;
import com.kingdee.bos.dao.query.QueryExecutorFactory;
import com.kingdee.bos.metadata.IMetaDataPK;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.common.client.SysContext;
import com.kingdee.eas.jky.JkyHeperFacadeFactory;
import com.kingdee.eas.jky.helper.JKYHelper;
import com.kingdee.eas.scm.sm.pur.PurOrderFactory;
import com.kingdee.eas.scm.sm.pur.PurOrderInfo;

public class PurOrderListUICTEx extends PurOrderListUI {

	private static final long serialVersionUID = -2258485084302428162L;

	public PurOrderListUICTEx() throws Exception {
		super();
		
	}

	@Override
	protected boolean initDefaultFilter() {
		if (getUIContext().get("nextID") != null) {
			getUIContext().put(getMainBizOrgType(), SysContext.getSysContext().getCurrentAdminUnit());
			return false;
		} else {
			return super.initDefaultFilter();
		}
	}
	@Override
	protected IQueryExecutor getQueryExecutor(IMetaDataPK queryPK,
			EntityViewInfo viewInfo) {
		if (getUIContext().get("nextID") != null) {
			IQueryExecutor exec = null;
			FilterInfo filterInfo = new FilterInfo();
			Set setID = (Set) getUIContext().get("nextID");
			String projectID = null;
			Iterator it = setID.iterator();
			while (it.hasNext()) {
				projectID = (String) it.next();
				break;
			}
			if (projectID != null) {
				try {
					Set sets = JKYHelper.getPojectNextBillIDS("T_SM_PurOrder",
							"CFProjectID", "'" + projectID + "'");
					filterInfo.getFilterItems()
							.add(
									new FilterItemInfo("id", sets,
											CompareType.INCLUDE));
					viewInfo = new EntityViewInfo();
					viewInfo.setFilter(filterInfo);
					exec = QueryExecutorFactory.getRemoteInstance(queryPK);
					exec.setObjectView(viewInfo);
				} catch (Exception e) {

					e.printStackTrace();
				}
			}
			return exec;
		} else {
			return super.getQueryExecutor(queryPK, viewInfo);
		}
	}
	@Override
	public FilterInfo getFilterInfo() {
		if (getUIContext().get("nextID") != null) {
			return new FilterInfo();
		} else {
			return super.getFilterInfo();
		}
	}

	@Override
	protected FilterInfo getDefaultFilterForQuery() {
		if (getUIContext().get("nextID") != null) {
			return new FilterInfo();
		} else {
			return super.getDefaultFilterForQuery();
		}

	}

	@Override
	protected boolean isIgnoreCUFilter() {
		if (getUIContext().get("nextID") != null) {
			return true;
		} else {
			return super.isIgnoreCUFilter();
		}
	}
	public void onLoad() throws Exception {

		super.onLoad();
		this.btnAudit.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				beforeActionPerformed(e);
				try {
					btnAudit_actionPerformed(e);
				} catch (Exception exc) {
					handUIException(exc);
				} finally {
					afterActionPerformed(e);
				}
			}
		});
		this.btnUnAudit.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				beforeActionPerformed(e);
				try {
					btnUnAudit_actionPerformed(e);
				} catch (Exception exc) {
					handUIException(exc);
				} finally {
					afterActionPerformed(e);
				}
			}
		});

	}

	protected void btnAudit_actionPerformed(ActionEvent e)
			throws EASBizException {
		try {
			checkSelected();
			String id = this.getSelectedKeyValue();
			PurOrderInfo perInfo = PurOrderFactory.getRemoteInstance()
					.getPurOrderInfo(new ObjectUuidPK(id));
			JkyHeperFacadeFactory.getRemoteInstance().subMaterial(perInfo);
		} catch (BOSException e1) {
			e1.printStackTrace();
		}
	}

	protected void btnUnAudit_actionPerformed(ActionEvent e)
			throws EASBizException {
		// try {
		// String id=this.getSelectedKeyValue();
		// PurOrderInfo perInfo
		// =PurOrderFactory.getRemoteInstance().getPurOrderInfo(new
		// ObjectUuidPK(id));
		// JkyHeperFacadeFactory.getRemoteInstance().backMaterial(perInfo);
		// } catch (BOSException e1) {
		// e1.printStackTrace();
		// }
	}

	public void actionUnAudit_actionPerformed(ActionEvent e) throws Exception {
		super.actionUnAudit_actionPerformed(e);
		//JkyHeperFacadeFactory.getRemoteInstance().backMaterial(PurOrderFactory
		// .getRemoteInstance().getPurOrderInfo(new
		// ObjectUuidPK(this.getSelectedKeyValue())));
	}

	public void actionAudit_actionPerformed(ActionEvent e) throws Exception {
		super.actionAudit_actionPerformed(e);
		checkSelected();
		//JkyHeperFacadeFactory.getRemoteInstance().subMaterial(PurOrderFactory.
		// getRemoteInstance().getPurOrderInfo(new
		// ObjectUuidPK(this.getSelectedKeyValue())));
	}
}